<?php

namespace App\Http\Validate\CallingCards\Article;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\FeishuCallingCard\FeishuCallingCardsArticle;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function Symfony\Component\String\s;

class HomeArticleValidate extends AbstractBaseValidate
{


    /**
     * @inheritDoc
     */
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [
            "type"                   => "int|required|in:" . enumTransToRules(FeishuCallingCardsArticle::ARTICLE_FROM_TEXT[FeishuCallingCardsArticle::ARTICLE_FROM_2]),
            "name"                   => "string|required|min:1|max:60",
            "originalImage"          => "string|required",
            "thumbImage"             => "string|required",
            "url"                    => "string|required",
            "content"                => "string|required",
            "weight"                 => "int|required",
            "extraVideo"             => "string|required",
            "extraCityText"          => "string|required",
            "extraShopTypeName"      => "string|required",
            "extraArea"              => "int|required",
            "extraStartBusinessTime" => "string|date_format:Y-m-d",
            "extraView"              => "int|required",
            "extraDetailImages"      => "required|array",
            "extraDetailContent"     => "required|string",
            "extraCreateTime"        => "string|date_format:Y-m-d H:i:s",
        ];
    }

    /**
     * @inheritDoc
     */
    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    /**
     * @inheritDoc
     */
    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    /**
     * @inheritDoc
     */
    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * @param array $requestParams
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function homeArticleAddValidate(array $requestParams)
    {
        $type = $requestParams["type"] ?? 0;
        list($validateFields, $extraRules, $handParams) = self::getEditOrInsertValidate($type);
        return self::getInstance()->validate(
            $validateFields,
            $requestParams,
            $extraRules,
            true,
            $handParams
        );
    }

    /**
     * @param array $requestParams
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function homeArticleEditFormValidate(array $requestParams)
    {
        return self::getInstance()->validate(
            ["id"],
            $requestParams,
            [
                "id" => Rule::exists("feishu_calling_cards_articles", "id")
            ]
        );
    }

    /**
     * @param array $requestParams
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function homeArticleEditValidate(array $requestParams)
    {
        $type = $requestParams["type"] ?? 0;
        list($validateFields, $extraRules, $handParams) = self::getEditOrInsertValidate($type);
        return self::getInstance()->validate(
            array_merge($validateFields, ["id"]),
            $requestParams,
            array_merge($extraRules, [
                "id" => Rule::exists("feishu_calling_cards_articles", "id")
            ]),
            true,
            $handParams
        );
    }

    /**
     * 获取编辑&新增表单验证规则
     *
     * @param int $type
     * @return array
     */
    public static function getEditOrInsertValidate(int $type): array
    {
        $validateFields = $extraRules = [];

        switch ($type) {
            case FeishuCallingCardsArticle::ARTICLE_TYPE_3:
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_31:
                $validateFields = ["type", "url", "weight", "originalImage"];
                $extraRules     = ["url" => "sometimes|string|min:0|max:255"];
                break;
            case FeishuCallingCardsArticle::ARTICLE_TYPE_4:
            case FeishuCallingCardsArticle::ARTICLE_TYPE_6:
                $validateFields = ["type", "weight", "originalImage"];
                break;
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_34:
                $validateFields = ["type", "weight", "originalImage", "content"];
                break;
            case FeishuCallingCardsArticle::ARTICLE_TYPE_10:
                $validateFields = ["type", "weight", "originalImage", "extraDetailImages"];
                break;
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_37:
                $validateFields = ["type", "name", "content", "weight", "originalImage", "thumbImage"];
                break;
            case FeishuCallingCardsArticle::ARTICLE_TYPE_9:
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_36:
                $validateFields = [
                    "type",
                    "name",
                    "weight",
                    "originalImage",
                    "url",
                    "thumbImage",
                    "extraCreateTime",
                    "content"
                ];
                break;
            case FeishuCallingCardsArticle::ARTICLE_TYPE_5:
                $validateFields = [
                    "type",
                    "name",
                    "weight",
                    "originalImage",
                    "extraDetailImages"
                ];
                break;
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_33:
                $validateFields = [
                    "type",
                    "name",
                    "weight",
                    "originalImage",
                    "extraDetailContent"
                ];
                break;
            case FeishuCallingCardsArticle::ARTICLE_TYPE_7:
                $validateFields = [
                    "type",
                    "weight",
                    "extraDetailImages",
                    "originalImage",
                ];
                break;
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_38:
                $validateFields = [
                    "type",
                    "weight",
                    "extraDetailContent",
                    "originalImage",
                ];
                break;
            case FeishuCallingCardsArticle::ARTICLE_TYPE_8:
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_35:
                $validateFields = [
                    "type",
                    "originalImage",
                    "name",
                    "content",
                    "extraVideo",
                    "weight"
                ];
                break;
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_32:
                $validateFields = [
                    "type",
                    "originalImage",
                    "name",
                    "content",
                    "weight"
                ];
                break;
            case FeishuCallingCardsArticle::ARTICLE_PC_TYPE_39:
                $validateFields = [
                    "type",
                    "originalImage"
                ];
                break;
            default:
                $validateFields = [
                    "type"
                ];
                break;
        }
        return [
            $validateFields,
            $extraRules,
            function (array $requestParams) {
                $extra = [];
                collect($requestParams)->each(function ($v, $k) use (&$extra, &$requestParams) {
                    switch ($k) {
                        case "originalImage":
                        case "extraVideo":
                        case "thumbImage":
                            $v                 = getPrivateFileKey($v);
                            $requestParams[$k] = $v;
                            break;
                        case "extraStartBusinessTime":
                        case "extraCreateTime":
                            $v = strtotime($v);
                            break;
                        case "extraDetailImages":
                            $v = array_map("getPrivateFileKey", $v);
                            break;
                    }

                    if (Str::contains($k, "extra")) {
                        $extra = array_merge($extra, [$k => $v]);
                        unset($requestParams[$k]);
                    }
                });

                return (object)array_merge(
                    $requestParams,
                    ["extra" => json_encode($extra, JSON_UNESCAPED_UNICODE)]
                );
            }
        ];
    }
}
